<?php
use Workerman\Worker;
use Workerman\Lib\Timer;
use Beanstalk\Client;
require_once __DIR__ . '/Workerman/Autoloader.php';
require_once __DIR__ . '/Channel/src/Server.php';
require_once __DIR__ . '/Channel/src/Client.php';
require_once __DIR__ . '/mysql/src/Connection.php';  //mysql组件
require_once __DIR__ . '/Beanstalk/Client.php';

// 创建一个Worker监听2345端口，使用http协议通讯
$worker = new Worker("websocket://172.17.73.202:9090");
// 设置实例的名称
$worker->name = 'beanstalkd';
// 启动4个进程对外提供服务
$worker->count = 10;

$worker->onWorkerStart = function($worker) {
    Channel\Client::connect('127.0.0.1', 2206);
    //创建数据框连接
    global $db;
    $db = new \Workerman\MySQL\Connection('rm-uf6oi8jxh9hkx6xaeo.mysql.rds.aliyuncs.com', '3306', 'liuyu', 'SUIbian1995', 'bento');
    //定义消息队列处理函数
    $config = [
        'persistent' => false, //是否长连接
        'host' => '47.95.201.26',
        'port' => 11300,  //端口号默认11300
        'timeout' =>30   //连接超时时间
    ];
    $Client=new Client($config);  //实列化客户端
    if(!$Client->connect()){
        throw new \Exception('连接失败');
    }  //连接服务端
    $Client->useTube('test'); //使用通道
    $Client->watch('test'); //将tube添加到watch list，consumer从watch list中的tube获取job
    Timer::add(1, function()use($worker,$Client){
        $res=$Client->reserve();  //获取一个job
        $message_id=$res['id'];  //获取job id
        if ($message_id < 1) {
            return false;
        }
        var_dump($res);
        $Client->delete($message_id); //将job从任务中清除
    });
};
